<template>
  <div class="app-container">

    <div style="overflow: auto;padding-bottom: 20px;">
      <el-steps :active="active" :direction="'horizontal'" :space="180" finish-status="success">
        <el-step style="flex-shrink: 0; max-width: none;" v-for="(item, index) in workprocesslist"
          :key="index"
          :title="item.flowname"
        >
          <template slot="description">
            <span>应交金额：{{item.planmoney}}元</span><br/>
            <span>计划时间：{{parseTime(item.starttime, '{y}-{m}-{d}')}}</span>
          </template>
        </el-step>
      </el-steps>
    </div>

    <el-form ref="form" :model="form" :rules="rules" label-width="120px" :inline="true" style="margin-top: 20px;" v-if="!finish">
      <el-form-item label="当前流程" prop="flowname">
        <el-input v-model="form.flowname" readonly />
      </el-form-item>
      <el-form-item label="应交金额" prop="planmoney">
        <el-input v-model="form.planmoney" readonly/>
      </el-form-item>
      <el-form-item label="计划完成时间" prop="starttime">
        <el-input v-model="form.starttime" value-format="yyyy-MM-dd" readonly/>
      </el-form-item>
      <el-form-item label="关键节点" prop="flownode">
        <el-input v-model="form.flownode" readonly />
      </el-form-item>
      <el-form-item label="实交金额" prop="factmoney">
        <el-input v-model="form.factmoney" placeholder="请输入实交金额" type="number"/>
      </el-form-item>
      <el-form-item label="实际完成时间" prop="finishtime">
        <el-date-picker clearable
          v-model="form.finishtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际完成时间">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div style="text-align: right;" v-if="!finish">
      <el-button type="primary" @click="processlist">修改购房流程</el-button>
      <el-button type="primary" @click="submitForm">完成当前流程</el-button>
    </div>

    <!-- 修改购房流程对话框 -->
    <el-dialog title="购房流程列表" :visible.sync="open" width="89%" append-to-body>
      <Workprocess :hourseid="form.hourseid" @next="handleNext"/>
    </el-dialog>
  </div>
</template>

<script>
  import { listWorkProcess, updateWorkProcess } from "@/api/house_sale/WorkProcess";
  import Workprocess from '@/views/house_sale/components/workprocess/index';

  export default {
    name: 'step',
    components: {
      Workprocess
    },
    props: ["hourseid","refresh"],
    data() {
      return {
        // 购房流程是否结束
        finish: false,
        open: false,
        active: 0,
        workprocesslist: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 500,
          hourseid: null,
        },
        form: {},
        rules: {
          factmoney: [
            { required: true, message: "实交金额不能为空", trigger: "blur" }
          ],
          finishtime: [
            { required: true, message: "实际完成时间不能为空", trigger: "blur" }
          ]
        }
      }
    },
    created() {
      this.queryParams.hourseid = this.hourseid;
      this.getList();
    },
    watch: {
      refresh() {
        this.queryParams.hourseid = this.hourseid;
        this.getList();
      }
    },
    methods: {
      /** 查询购房流程列表 */
      getList() {
        listWorkProcess(this.queryParams).then(response => {
          this.workprocesslist = response.rows;
          for(var i=0;i<this.workprocesslist.length;i++) {
            let finished = true;
            if(this.workprocesslist[i].status == '未完成') {
              this.finish = false;
              finished = false;
              this.active = i;
              this.form = this.workprocesslist[i];
              break;
            }
            if(finished) {
              this.active = this.workprocesslist.length;
              this.finish = true;
            }
          }
        });
      },
      // 显示购房流程列表
      processlist() {
        this.open = true;
      },
      handleNext() {
        this.open = false;
        this.getList();
      },
      /** 完成当前流程 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.status = '已完成';
            updateWorkProcess(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.getList();
              this.$emit("next");
            });
          }
        })
      },
    }
  }
</script>
